using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

using EntitySpaces.Interfaces;

using AutoBase.BusinessObjects;
using AutoBase.Shared;

namespace Autobase.WebControl.Managers
{
    public static class VehicleSearchManager
    {
        public enum OrderBy
        {
            ASC=0,DESC
        }
 

        public static ViewDistinctManufacturerModelByBranchIdCollection LoadManufacturer(
                        List<string> BranchIdLocationList,
                        int? BranchId)
        {

            ViewDistinctManufacturerModelByBranchIdCollection
                manufacturers = new ViewDistinctManufacturerModelByBranchIdCollection();

            manufacturers.Query.Select
            (
                manufacturers.Query.ManufacturerId,
                manufacturers.Query.ManufacturerName
            );

            if (BranchIdLocationList.Count > 0)
            {
                manufacturers.Query.Where(manufacturers.Query.BranchId.In(BranchIdLocationList.ToArray()));
            }

            if (BranchId != null && BranchIdLocationList==null &&
                BranchIdLocationList.Count==0 && Convert.ToInt64(BranchId) > 0)
            {
                manufacturers.Query.Where(manufacturers.Query.BranchId.Equal(BranchId));
            }

            manufacturers.Query.Where(manufacturers.Query.ManufacturerId.GreaterThan(0));

            manufacturers.Query.es.Distinct = true;
            manufacturers.Query.OrderBy(manufacturers.Query.ManufacturerName.Ascending);

            manufacturers.Query.Load();

            return manufacturers;
        }



        public static ViewDistinctManufacturerModelByBranchIdCollection LoadModels(
                        int ManufacturerId,
                        List<string> BranchIdLocationList,
                        int? BranchId)
        {
            ViewDistinctManufacturerModelByBranchIdCollection 
                models = new ViewDistinctManufacturerModelByBranchIdCollection();

            models.Query.Select
                (
                    models.Query.ModelID
                    , models.Query.ModelName
                );

            models.Query.Where(models.Query.ModelID.GreaterThan(0));

            models.Query.Where(models.Query.ManufacturerId.Equal(ManufacturerId));

            if (BranchIdLocationList.Count > 0)
            {
                models.Query.Where(models.Query.BranchId.In(BranchIdLocationList.ToArray()));
            }

            if (BranchId != null && BranchIdLocationList == null &&
                BranchIdLocationList.Count == 0 && Convert.ToInt64(BranchId) > 0)
            {
                models.Query.Where(models.Query.BranchId.Equal(BranchId));
            }


            models.Query.es.Distinct = true;
            models.Query.OrderBy(models.Query.ModelName.Ascending);

            models.Query.Load();

            return models;
        }

        public static ViewVehiclesRetailCollection LoadVehicles()
        {
            ViewVehiclesRetailCollection vehicleRetail = new ViewVehiclesRetailCollection();

            vehicleRetail = PrepairQuery(vehicleRetail,"",-1,"","",-1,-1,"","",-1,OrderBy.ASC,"");
            vehicleRetail.Query.es.Top = 1000;

            return vehicleRetail;

        }

        private static ViewVehiclesRetailCollection PrepairQuery(
                            ViewVehiclesRetailCollection vehicles,
                            string CustomizedFilterCode,
                            int ManufacturerId,
                            string BranchIdLocationList,
                            string PriceRange,
                            int CompanyId,
                            int BranchId,
                            string CategoryList,
                            string YearRange,
                            int ModelId,
                            OrderBy OrderBy,
                            string SortBy
                            )
        {

            short shortValFrom = -1;
            short shortValTo = -1;
            decimal decimalValFrom = -1;
            decimal decimalValTo = -1;
            System.Collections.ArrayList arrayWhereQuery = new System.Collections.ArrayList();
            System.Collections.ArrayList objectIdList = new System.Collections.ArrayList();
            char[] charsToTrim = { ',' };

            HiddenField uicConstructUrl = new HiddenField();
            uicConstructUrl.Value = string.Empty;

            #region Query

            #region Customized Filters
            switch (CustomizedFilterCode)
            {
                case "104":
                    arrayWhereQuery.Add(vehicles.Query.ModelDetail.Like("%Triple S%"));
                    break;

                case "demo":
                    arrayWhereQuery.Add(vehicles.Query.ModelDetail.Like("%DEMO%"));
                    break;

                case "signature":
                    arrayWhereQuery.Add(vehicles.Query.ModelDetail.Like("%signature%"));
                    break;

                case "nonsignature":
                    arrayWhereQuery.Add(vehicles.Query.ModelDetail.NotLike("%signature%"));
                    break;

                case "bmw3":
                    ManufacturerId = 9;
                    arrayWhereQuery.Add(vehicles.Query.ModelName.Like("3%"));
                    break;

                case "bmw7":
                    ManufacturerId = 9;
                    arrayWhereQuery.Add(vehicles.Query.ModelName.Like("7%"));
                    break;

                case "bmwx5":
                    ManufacturerId = 9;
                    arrayWhereQuery.Add(vehicles.Query.ModelName.Like("X5%"));
                    break;

                case "bmwz3":
                    ManufacturerId = 9;
                    arrayWhereQuery.Add(vehicles.Query.ModelName.Like("Z3%"));
                    break;

                case "under10k":
                    PriceRange = "0|10000";
                    break;

                case "newonly":
                    arrayWhereQuery.Add(vehicles.Query.IsNew.Equal(true));
                    break;

                case "usedonly":
                    arrayWhereQuery.Add(vehicles.Query.IsNew.Equal(false));
                    break;

                case "specials":
                    arrayWhereQuery.Add(vehicles.Query.IsSpecial.Equal(true));
                    break;

                case "ssyng":
                    ManufacturerId = 78;
                    break;
                default:
                    break;
            }

            #endregion

            int objectId = 0;
            if (BranchIdLocationList.Length > 0 && BranchIdLocationList.Split('|').Length > 0)
            {
                foreach (string id in BranchIdLocationList.Split('|'))
                {
                    if (id.Split('~').Length > 0)
                    {
                        int.TryParse(id.Split('~')[0], out objectId);
                    }
                    else
                    {
                        int.TryParse(id, out objectId);
                    }
                    if (objectId > 0)
                    {
                        objectIdList.Add(objectId);
                    }
                }
                if (objectIdList.Count > 0)
                {
                    arrayWhereQuery.Add(vehicles.Query.BranchId.In(objectIdList.ToArray()));
                }
            }
            else if (CompanyId > 0)
            {
                arrayWhereQuery.Add(vehicles.Query.CompanyId.Equal(CompanyId));
            }
            else if (BranchId > 0)
            {
                arrayWhereQuery.Add(vehicles.Query.BranchId.Equal(BranchId));

            }


            //Category List 
            objectIdList = new System.Collections.ArrayList();
            objectId = 0;
            if (CategoryList.Length > 0)
            {
                if (CategoryList.Split('|').Length > 0)
                {
                    foreach (string id in CategoryList.Split('|'))
                    {
                        if (id.Split('~').Length > 0)
                        {
                            int.TryParse(id.Split('~')[0], out objectId);
                        }
                        else
                        {
                            int.TryParse(id, out objectId);
                        }
                        if (objectId > 0)
                        {
                            objectIdList.Add(objectId);
                        }
                    }
                    if (objectIdList.Count > 0)
                    {
                        arrayWhereQuery.Add(vehicles.Query.CategoryId.In(objectIdList.ToArray()));
                    }
                }
                else
                {
                    if (int.TryParse(CategoryList, out objectId))
                    {
                        arrayWhereQuery.Add(vehicles.Query.CategoryId.Equal(CategoryList));
                    }
                }
            }

            //Year
            shortValFrom = -1;
            shortValTo = -1;
            if (YearRange.Length > 0 && YearRange.Split('|').Length > 1)
            {
                if (YearRange.Split('|')[0].Length > 0)
                {
                    short.TryParse(YearRange.Split('|')[0], out shortValFrom);
                }
                if (YearRange.Split('|')[1].Length > 0)
                {
                    short.TryParse(YearRange.Split('|')[1], out shortValTo);
                }
                if (shortValFrom > 0 && shortValTo > 0)
                {
                    arrayWhereQuery.Add(vehicles.Query.Year.Between(shortValFrom, shortValTo));
                }
                else
                {
                    if (shortValFrom > 0)
                    {
                        arrayWhereQuery.Add(vehicles.Query.Year.GreaterThanOrEqual(shortValFrom));
                    }
                    if (shortValTo > 0)
                    {
                        arrayWhereQuery.Add(vehicles.Query.Year.LessThanOrEqual(shortValTo));
                    }
                }
            }

            //Price
            decimalValFrom = -1;
            decimalValTo = -1;

            if (PriceRange.Length > 0 && PriceRange.Split('|').Length > 1)
            {
                if (PriceRange.Split('|')[0].Length > 0)
                {
                    decimal.TryParse(PriceRange.Split('|')[0], out decimalValFrom);
                }
                if (YearRange.Split('|')[1].Length > 0)
                {
                    decimal.TryParse(PriceRange.Split('|')[1], out decimalValTo);
                }
                if (decimalValFrom > 0 && decimalValTo > 0)
                {
                    arrayWhereQuery.Add(vehicles.Query.RetailPrice.Between(decimalValFrom, decimalValTo));
                }
                else
                {
                    if (decimalValFrom > 0)
                    {
                        arrayWhereQuery.Add(vehicles.Query.RetailPrice.GreaterThanOrEqual(decimalValFrom));
                    }
                    if (decimalValTo > 0)
                    {
                        arrayWhereQuery.Add(vehicles.Query.RetailPrice.LessThanOrEqual(decimalValFrom));
                    }
                }
            }

            //Manufactuere
            if (ManufacturerId > 0)
            {
                arrayWhereQuery.Add(vehicles.Query.ManufacturerId.Equal(ManufacturerId));
            }

            //Model
            if (ModelId > 0)
            {
                arrayWhereQuery.Add(vehicles.Query.ModelId.Equal(ModelId));
            }

            #endregion Query

            # region SortBy

            EntitySpaces.Interfaces.esOrderByDirection orderBy = EntitySpaces.Interfaces.esOrderByDirection.Ascending;
            string sortBy = "-1";

            /*
            if (uicOrder.SelectedIndex > 0)
            {
                orderBy = uicOrder.SelectedItem.Value == "desc" ? EntitySpaces.Interfaces.esOrderByDirection.Descending : EntitySpaces.Interfaces.esOrderByDirection.Ascending;
            }
            if (uicSortBy.SelectedIndex > 0)
            {
                sortBy = uicSortBy.SelectedItem.Value;
            }
             * */


            orderBy = OrderBy.ToString().Equals("DESC") ? EntitySpaces.Interfaces.esOrderByDirection.Descending : EntitySpaces.Interfaces.esOrderByDirection.Ascending;

            if (!string.IsNullOrEmpty(SortBy))
            {
                sortBy = SortBy;
            }


            switch (sortBy)
            {
                case "1":
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.ManufacturerName, orderBy);
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.ModelName, orderBy);
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.ModelDetail, orderBy);
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.Year, orderBy);
                    break;

                case "2":
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.Year, orderBy);
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.ManufacturerName, orderBy);
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.ModelName, orderBy);
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.ModelDetail, orderBy);
                    break;

                case "3":
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.VehicleTypeName, orderBy);
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.ManufacturerName, orderBy);
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.ModelName, orderBy);
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.ModelDetail, orderBy);
                    break;

                case "4":
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.CcRating, orderBy);
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.ManufacturerName, orderBy);
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.ModelName, orderBy);
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.ModelDetail, orderBy);
                    break;

                case "5":
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.Odometer, orderBy);
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.ManufacturerName, orderBy);
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.ModelName, orderBy);
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.ModelDetail, orderBy);
                    break;

                case "6":
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.ExtColour, orderBy);
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.ManufacturerName, orderBy);
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.ModelName, orderBy);
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.ModelDetail, orderBy);
                    break;

                case "7":
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.RetailPrice, orderBy);
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.ManufacturerName, orderBy);
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.ModelName, orderBy);
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.ModelDetail, orderBy);
                    break;

                case "8":
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.Transmission, orderBy);
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.ManufacturerName, orderBy);
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.ModelName, orderBy);
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.ModelDetail, orderBy);
                    break;

                case "9":
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.Length, orderBy);
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.ManufacturerName, orderBy);
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.ModelName, orderBy);
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.ModelDetail, orderBy);
                    break;

                case "10":
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.Id, orderBy);
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.ManufacturerName, orderBy);
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.ModelName, orderBy);
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.ModelDetail, orderBy);
                    break;

                default:
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.ManufacturerName, orderBy);
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.ModelName, orderBy);
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.ModelDetail, orderBy);
                    vehicles.Query.OrderBy(ViewVehiclesMetadata.ColumnNames.Year, orderBy);
                    break;
            }
            # endregion

            if (arrayWhereQuery.Count > 0)
            {
                vehicles.Query.Where(arrayWhereQuery.ToArray());
                // Load data
                vehicles.Query.Load();
#if (DEBUG)
                System.Diagnostics.Debug.WriteLine(vehicles.Query.es.LastQuery);
#endif
            }
            return vehicles;

        }

    }
}
